package com.example.andylau96.res.Function.Cashier;

import android.app.AlertDialog;
import android.app.Fragment;
import android.content.DialogInterface;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

import com.example.andylau96.res.DataBase.CashierDataBase;
import com.example.andylau96.res.Main.MainActivity;
import com.example.andylau96.res.R;


import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by AndyLau96 on 2016/12/18.
 */
public class erjiFragment extends Fragment implements View.OnClickListener, AdapterView.OnItemClickListener {
    private ListView erji_list;
    private SimpleAdapter adapter;
    private SQLiteDatabase db;
    private ImageButton re;
    private Button ok;
    private TextView v;
    private EditText text;
    private LinearLayout title;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.erji,container,false);
        erji_list= (ListView) view.findViewById(R.id.erji_list);
        adapter=new SimpleAdapter(getActivity(),getData(),R.layout.choose_food_item,new String[]{"name","id","num"},new int[]{
                R.id.food_name,R.id.food_price,R.id.food_num});
        erji_list.setAdapter(adapter);
        re= (ImageButton) view.findViewById(R.id.erji_title_return);
        ok= (Button) view.findViewById(R.id.erji_title_ok);
        title= (LinearLayout) view.findViewById(R.id.erji_title);
        erji_list.setOnItemClickListener(this);
        re.setOnClickListener(this);
        ok.setOnClickListener(this);
        return view;
    }
    public List<Map<String,Object>> getData()
    {
        List<Map<String,Object>> list=new ArrayList<Map<String,Object>>() ;
        db=new CashierDataBase(getActivity().getApplication()).getReadableDatabase();
        Cursor re=db.rawQuery("select *from Erji",null);
        re.moveToFirst();
        while(!re.isAfterLast())
        {
            String name=re.getString(re.getColumnIndex("itemName"));
            int id=re.getInt(re.getColumnIndex("itemID"));
            int num=re.getInt(re.getColumnIndex("itemNum"));
            Map<String,Object> map=new HashMap<>();
            map.put("name",name);
            map.put("id",id);
            map.put("num",num);
            list.add(map);
            re.moveToNext();
        }
        re.close();
        db.close();
        return list;
    }
    @Override
    public void onClick(View view) {
        int id=view.getId();
        if(id==R.id.erji_title_return)
        {
            getFragmentManager().popBackStack();
        }
        if(id==R.id.erji_title_ok)
        {
            LinearLayout n= (LinearLayout) erji_list.getChildAt(0);
            int i=0;
            while(n!=null)
            {
                TextView mid= (TextView) n.findViewById(R.id.food_price);
                TextView mnum= (TextView) n.findViewById(R.id.food_num);
                db=new CashierDataBase(getActivity().getApplication()).getWritableDatabase();
                db.execSQL("update Erji set itemNum="+mnum.getText().toString()+" where itemID="+mid.getText().toString());
                i++;
                n= (LinearLayout) erji_list.getChildAt(i);
            }
            db.close();
            Toast.makeText(getActivity(), "保存成功", Toast.LENGTH_SHORT).show();
            getFragmentManager().popBackStack();
        }
    }
    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l)
    {
        v= (TextView) view.findViewById(R.id.food_num);
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        text = new EditText(getActivity());
        builder.setTitle("请输入剩余库存量");
        builder.setIcon(android.R.drawable.ic_dialog_info);
        builder.setView(text);
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                String number=text.getText().toString();
                if(number.length()==0)
                    Toast.makeText(erjiFragment.this.getActivity(), "请输入剩余库存量", Toast.LENGTH_SHORT).show();
                else
                {
                    v.setText(number);
                    dialogInterface.dismiss();
                }
            }
        });
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                dialogInterface.dismiss();
            }
        });
        builder.show();
    }
    @Override
    public void onStart() {
        super.onStart();
        MainActivity.now=this;
        String theme=null;
        File file=new File("/data/data/com.example.andylau96.res/files/color.txt");
        try {
            InputStreamReader reader=new InputStreamReader(new FileInputStream(file),"UTF-8");
            BufferedReader br=new BufferedReader(reader);
            theme="";
            String temp=null;
            while((temp=br.readLine())!=null)
            {
                theme+=temp;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        title.setBackgroundColor(Color.parseColor(theme));
    }
}

